import { reqRegister } from "../../request/api"
import {Toast} from 'antd-mobile'

const initState = {
    user: {
        phone: "",
        nickname:"",
        password:""
    }
}

const TYPES={
    CHANGE_REGISTER_USER:"CHANGE_REGISTER_USER",
}

export const actions = {
    change_register_user:(value,key)=>({type:TYPES.CHANGE_REGISTER_USER,value,key}),
    reqRegister:(router) => {
        return (dispatch,getState)=>{
            let {user} = getState().register
            if(user.phone === "" || user.password === "" || user.nickname === ""){
                Toast.info("手机号，密码，昵称均不能为空")
                return
            }
            reqRegister(user).then(res=>{
                if(res.code===200){
                    Toast.info(res.msg)
                    router.push('/login')
                }
            })
        }
    }
}

const reducer = (state=initState,action)=>{
    switch (action.type) {
        case TYPES.CHANGE_REGISTER_USER:
            return {
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
        default:
            return state
    }
}
export default reducer

export const getUser = (state) => state.register.user